package com.cmol.peopledetectapp.admin;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 用户管理
 *
 * @author 王国良
 */
public class UserManageActivity extends AppCompatActivity {
    /**
     * 布局文件
     */
    private TextView emailText, passwordText;
    /**
     * 新账号
     */
    private EditText account;
    /**
     * 新密码
     */
    private EditText password;
    /**
     * 确认按钮
     */
    private Button sureChange;
    private Button Delete;
    /**
     * 通信模块
     */
    private Network network;
    private String thenAccount = "";
    private String thenPassword = "";
    private String currentAccount = "";
    private String currentPassword = "";
    private String currentId = "";
    private String succeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usermanage);
        emailText = findViewById(R.id.textView21);
        passwordText = findViewById(R.id.textView22);
        account = findViewById(R.id.account);
        password = findViewById(R.id.passwd);
        sureChange = findViewById(R.id.sureChange);
        Delete = findViewById(R.id.Delete);
        init();

        sureChange.setOnClickListener(new sureClickListener());
        Delete.setOnClickListener(new DeleteClickListener());
    }

    /**
     * 初始化函数
     */
    public void init() {
        Intent intent = getIntent();
        currentAccount = intent.getStringExtra("account");
        currentPassword = intent.getStringExtra("password");
        currentId = intent.getStringExtra("id");
        emailText.setText(currentAccount);
        passwordText.setText("密码：" + currentPassword);
    }

    /**
     * 确认提交
     *
     * @param view
     */
    public void submit(View view) {
        warnDia(view);
    }

    /**
     * 警告框
     *
     * @param view
     */
    public void warnDia(View view) {
        //创建一个警告对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(UserManageActivity.this);
        builder.setTitle("提示");
        builder.setMessage("确认保存当前修改？");

        AlertDialog.Builder builder1 = builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /**
                 *  向服务器反馈修改信息
                 */
                // 获取修改后的账号密码
                thenAccount += account.getText().toString();
                thenPassword += password.getText().toString();
                JSONObject tmp = new JSONObject();
                try {
                    tmp.put("id", currentId);
                    tmp.put("account", thenAccount);
                    tmp.put("password", thenPassword);
                    System.out.println("报文" + tmp);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                System.out.println("发送信息...............");
                // 发送并获得反馈
                network = new Network(tmp, "/admin/userAlter") {
                    @Override
                    protected void process(Call call, Response response) throws IOException {
                        String ans = Objects.requireNonNull(response.body()).string().trim();
                        /**
                         * auth :
                         */
                        try {
                            JSONObject js = new JSONObject(ans);
                            succeed = js.getString("auth");
//                            boolean success = Boolean.getBoolean(succeed);
//                            if (succeed.equals(true)) {
//                                Toast.makeText(UserManageActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
//                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void failureWarn() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(UserManageActivity.this, "用户修改异常", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                };

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (succeed.equals("true")) {
                    Toast.makeText(UserManageActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                }
            }

        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog alertDialog = builder.create();//这个方法可以返回一个alertDialog对象
        alertDialog.show();
    }

    /**
     * 确认提交
     *
     * @param view
     */
    public void submit2(View view) {
        warnDia2(view);
    }

    /**
     * 警告框
     *
     * @param view
     */
    public void warnDia2(View view) {
        //创建一个警告对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(UserManageActivity.this);
        builder.setTitle("提示");
        builder.setMessage("确认注销当前用户？");

        AlertDialog.Builder builder1 = builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /**
                 *  向服务器反馈修改信息
                 */
                // 获取修改后的账号密码
                JSONObject tmp = new JSONObject();
                try {
                    tmp.put("id", currentId);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

//                System.out.println("发送信息...............");
                // 发送并获得反馈
                network = new Network(tmp, "/admin/Delete") {
                    /**
                     *
                     * @param call 响应
                     * @param response 响应报文
                     * @throws IOException 未处理异常
                     */
                    @Override
                    protected void process(Call call, Response response) throws IOException {
                        String ans = Objects.requireNonNull(response.body()).string().trim();
                        /**
                         * auth :
                         */
                        try {
                            JSONObject js = new JSONObject(ans);
                            succeed = js.getString("auth");
//                            if (succeed.equals(true)) {
//                                Toast.makeText(UserManageActivity.this, "注销成功", Toast.LENGTH_SHORT).show();
//                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    /**
                     * 网络断开
                     */
                    @Override
                    protected void failureWarn() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(UserManageActivity.this, "注销异常", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                };
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (succeed.equals("true")) {
                    Toast.makeText(UserManageActivity.this, "注销成功", Toast.LENGTH_SHORT).show();
                }

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });


        AlertDialog alertDialog = builder.create();//这个方法可以返回一个alertDialog对象
        alertDialog.show();
    }

    /**
     * 点击修改用户和密码
     */
    class sureClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.sureChange) {
                submit(view);
            }
        }
    }

    // 注销用户
    class DeleteClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.Delete) {
                submit2(view);
            }
        }
    }
}